'use strict';
angular.module('gkwb')
    .factory('Student', ['$window', '$http', '$cacheFactory', '$q', function($window, $http, $cacheFactory, $q) {
        var cache = $cacheFactory('cache_student');
        return {
            get: function(examId) {
                var defered = $q.defer();
                var stu = cache.get('stu');
                if (!stu) {
                    $http.get('/api/students/' + examId)
                        .success(function(res) {
                            res.examId = examId;
                            defered.resolve(res);
                            cache.put('stu', res);
                        });
                } else {
                    defered.resolve(stu);
                }
                return defered.promise;
            },
            getAll: function() {
                var defered = $q.defer();
                $http.get('/api/students')
                    .success(function(res) {
                        defered.resolve(res);
                    });
                return defered.promise;
            },
            save: function(student) {
                var defered = $q.defer();
                $http.post('/api/students', student)
                    .success(function(res) {
                        defered.resolve(res);
                    })
                    .error(function(err) {
                        if (err)
                            defered.reject(err.message);
                    })
                return defered.promise;
            },
            reset: function() {
                cache.remove('stu');
                var stu = cache.get('stu');
                console.log(stu);
            }
        }
    }])
    .factory('SignCard', ['$http','$q', function($http,$q) {


        return {
            query:function(groupId,groupType,isCreated){
                var url = '/api/admin/signcards?groupId=' + groupId + '&groupType=' + groupType;
                if(isCreated != undefined){
                    url += '&isCreated=' + isCreated;
                }
                var defered = $q.defer();
                $http.get(url)
                .success(function(res) {
                        defered.resolve(res);
                    })
                    .error(function(err) {
                        defered.reject(err);
                    })
                return defered.promise;
            },
            create:function(signCardRecord){
                var url = '/api/admin/signcards';
                var defered = $q.defer();
                $http.post(url,signCardRecord)
                .success(function(res) {
                        defered.resolve(res);
                    })
                    .error(function(err) {
                        defered.reject(err);
                    })
                return defered.promise;
            },
            generate:function(signCardRecordId){
                var url = '/api/admin/signcards/'+signCardRecordId;
                var defered = $q.defer();
                $http.put(url)
                .success(function(res) {
                        defered.resolve(res);
                    })
                    .error(function(err) {
                        defered.reject(err);
                    })
                return defered.promise;
            },
            remove:function(signCardRecordId){
                var url = '/api/admin/signcards/'+signCardRecordId;
                var defered = $q.defer();
                $http.delete(url)
                .success(function(res) {
                        defered.resolve(res);
                    })
                    .error(function(err) {
                        defered.reject(err);
                    })
                return defered.promise;
            }
        }
    }])
    .factory('KS', ['$resource', function($resource) {


        return $resource('/view/ks/:id', {
            id: '@id'
        });
    }])
    .factory('Order', ['$http', '$q', function($http, $q) {

        return {
            query: function(examId) {
                var url = '/api/orders?examId=' + examId;
                var defered = $q.defer();
                $http.get(url)
                    .success(function(res) {
                        defered.resolve(res);
                    })
                    .error(function(err) {
                        defered.reject(err);
                    })
                return defered.promise;
            },
            queryFromPrimary: function(options, examId) {
                var options = options || {
                    page: 0,
                    size: 10,
                    sort: '',
                    order: 'asc'
                };
                var params = '?page=' + options.page + '&size=' + options.size;
                if (options.examId) {
                    params += '&examId=' + options.examId;
                }
                var url = '/api/orders/zfmx' + params;
                var defered = $q.defer();
                $http.get(url)
                    .success(function(res) {
                        defered.resolve(res);
                    })
                    .error(function(err) {
                        defered.reject(err);
                    })
                return defered.promise;
            }
        }
    }]);